package com.example.huahanshu2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class EView extends View {
    private Point mCoo = new Point(500, 900);//坐标系
    private Paint mPaint;//主画笔
    private float n1=0;
    private float n2=0;
    private float n3=0;
    private int flag=1;
    public EView(Context context) {
        this(context, null);
    }
    public EView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();//初始化
    }
    private void init() {
        //初始化主画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        //初始化主路径
        //初始化辅助
    }
    private ArrayList<Float> n=new ArrayList<>();//定义域
    private HashMap<Float, Float> funMap = new HashMap<>();//映射表
    private void initDf() {
        for (float i = -400; i <= 500; i= (float) (i+0.001)) {
            n.add(i);//初始化定义域
        }
    }
    private Float f(Float x) {
        float y=n1*(float) Math.pow(n3,x*n2);
        return y;
    }
    private void map() {
        for(int i=0;i<n.size();i++)
        {
            float a=n.get(i);
            float b=f(a);
            funMap.put(a,b);
        }
        //添加所有点
    }


    private void initDf2() {
        for (float i = -400; i <= 500; i= (float) (i+0.001)) {
            n.add(i);//初始化定义域
        }
    }
    private Float f2(Float x) {
        float y=(float) ((n1*(float) Math.pow(n3,x*n2))*10);
        return y;
    }
    private void map2() {
        for(int i=0;i<n.size();i++)
        {
            float a=n.get(i);
            float b=f2(a);
            funMap.put(a,b);
        }
        //添加所有点
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void drawMap(Canvas canvas, Map<Float, Float> map) {
        map.forEach((k, v) -> {
            canvas.drawPoint(k, v, mPaint);
        });
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mCoo.x, mCoo.y);
        canvas.scale(1, -1);//y轴向上
        if(n1==0&&n2==0&&n3==0){}
        else {
            if(flag==1) {
                initDf();
                map();
                drawMap(canvas, funMap);
            }
            else{
                initDf2();
                map2();
                drawMap(canvas, funMap);
            }
        }
    }
    public float getN1() {
        return n1;
    }
    public void setN1(float n1) {
        this.n1 = n1;
    }

    public void setN2(float n2) { this.n2 = n2; }
    public float getN2() { return n2; }

    public float getN3() { return n3; }
    public void setN3(float n3) { this.n3 = n3; }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }
}
